<?php
/**存取钱*/
class ChangeCoinMoney extends APIBase{
	
	public $tag = "ChangeCoinMoney";
	public $isLogin = true;
	
	public function before() 
	{
		$this->initCacheRedis();
		$this->initMysql();
		return true;
	}
	
	public function logic() 
	{
		//coin_id  ,skey,uid, 
		if (!isset($this->param['coin_id'])) {
			$this->returnError(310, "coin_id must be needed");
		}
				
		$where['id'] = (int)$this->param['coin_id'];
		$coin = $this->mysql->select('change_coin_money','*',$where);
      
		if(!count($coin)) {     
			$this->returnError(310, "coin_id is not exists");
		}
		
		$ret = $this->data_redis->hMGet("hu:{$this->uid}", array('money','coin','online_vid'));
		
		
		$ret['money'] = (int) $ret['money'];
		$ret['coin'] = (int) $ret['coin'];
        //判断游戏中不能取钱
		if((int)$ret['online_vid']>0){
			$this->returnError(409, "正在游戏中,不能存取款");
		}
		
	    $need_coin = $coin[0]['coin'];//需要兑换元宝数
		
		if($need_coin>$ret['coin']){
			$this->returnError(410, "元宝数量不足，不能兑换");  
		}
		//开始兑换
		$ret['money'] += $coin[0]['money'];
		$ret['coin']  -= $coin[0]['coin'];
		//兑换日记
		//$result = $this->mysql->insert("changemoneyrecord",$binfo); 
        //兑换记录
		$m_rec['type'] = 4;
		/*$m_rec['vid'] = 0;
		$m_rec['tid']  = 0;
		$m_rec['seatid']  = 0;*/
		$m_rec['uid']  =  $this->uid;
		$m_rec['money'] = $ret['money'];
		$m_rec['change_money']  =  $coin[0]['money'];
		$this->addChangeMongeyLog($m_rec); 
		
		
		$this->data_redis->hMset("hu:{$this->uid}", $ret);
		$result = $this->data_redis->hMget("hu:{$this->uid}", array('money', 'coin'));
		$result['money'] = (int)$result['money'];
		$result['coin'] = (int)$result['coin'];
		$this->returnData($result,0, "兑换成功");


	}
	
	public function after() 
	{
		$this->deinitMysql();
		$this->deinitCacheRedis();
		$this->deinitDataRedis();
	}
}